package com.zhiyou100.review.day021.homework.homework3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @packageName: DownloadFile
 * @className: com.zhiyou100.review.day021.homework.homework3
 * @Description: TODO 服务器 处理文件
 * @author: yang
 * @date: 7/2/20
 */
public class DownloadFile {
    private static final String PATH = "/Users/yang/Downloads/test/accept";
    private static final int PORT = 10086;

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(PORT);
        // 创建 服务器

        BufferedWriter digit = new BufferedWriter(new FileWriter(new File(PATH, "digit.txt")));
        BufferedWriter letter = new BufferedWriter(new FileWriter(new File(PATH, "letter.txt")));
        BufferedWriter other = new BufferedWriter(new FileWriter(new File(PATH, "other.txt")));
        // 创建 3个 高效流 写入文件

        Socket socket = serverSocket.accept();
        // 获取 socket

        InputStream inputStream = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        StringBuilder stringBuilder = new StringBuilder();
        while (true) {
            String s = reader.readLine();
            // 读一行
            if (s == null) {
                // 读到末尾，退出
                break;
            }
            stringBuilder.append(s);
        }
        reader.close();
        // 关闭高效读取流

        digit.write(UploadFile.getDigest(stringBuilder.toString()));
        digit.flush();
        letter.write(UploadFile.getLetter(stringBuilder.toString()));
        letter.flush();
        other.write(UploadFile.getOther(stringBuilder.toString()));
        other.flush();
        // 分别写入三个文件，并刷新

        digit.close();
        letter.close();
        other.close();

        socket.close();
        // 关流


    }
}
